﻿using Siemens.Engineering;
using Siemens.Engineering.Hmi;
using Siemens.Engineering.HW;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 导入画面
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting TIA Portal");
            using (TiaPortal tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface)) //打开博途
            {
                Console.WriteLine("TIA Portal has started");
                ProjectComposition projects = tiaPortal.Projects;
                Console.WriteLine("Opening Project...");

                Console.WriteLine("输入项目文件路径");

                string projectFilePath = Console.ReadLine();

                FileInfo projectPath = new FileInfo(projectFilePath);

                Project _project = null;

                //打开项目
                try
                {
                    _project = projects.OpenWithUpgrade(projectPath);
                }
                catch (Exception)
                {
                    Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                    Console.WriteLine("Demo complete hit enter to exit");
                    Console.ReadLine();
                }

                //打开完成
                Console.WriteLine(String.Format("Project {0} is open", _project.Path.FullName));

                //枚举设备
                GetDevices.EnumerateDevicesInGroups(_project);
               
                for (int i = 1; i <= GetDevices.list.Count; i++)
                {
                    Console.WriteLine(i + ":" + GetDevices.list[i-1].Name);
                }

                Console.WriteLine("选择目标设备");

                int deviceNo = int.Parse(Console.ReadLine());

                Device device = GetDevices.list[deviceNo - 1];

                HmiTarget hmiTarget = GetHmiTarget.GetHmiTargetFromDevice(device);

                //输入画面文件路径
                Console.WriteLine("输入画面文件所在文件夹路径");

                string screenFileForld = Console.ReadLine();

                List<string> filePath = GetFilePath.Director(screenFileForld);

                foreach (string filePathItem in filePath)
                {
                    FileInfo screenFileInfo = new FileInfo(filePathItem);
                    hmiTarget.ScreenFolder.Screens.Import(screenFileInfo, ImportOptions.Override);
                }

                _project.Save();
                Console.WriteLine("Done");

                _project.Close();
                Console.WriteLine("Demo complete hit enter to exit");
                Console.ReadLine();
            }
        }
    }
}
